package com.qf.hospital.system.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class JedisUtil {

    @Autowired
    private JedisPool pool;

    /**
     * 像Redis存key-value的同时，给key设置生存时间
     * @param key
     * @param value
     * @param second
     */
    public void setex(byte[] key,byte[] value,Integer second){
        Jedis jedis = null;
        try {
            jedis = pool.getResource();

            jedis.setex(key,second,value);
        } finally {
            jedis.close();
        }
    }

    /**
     * 给key重新设置生存时间
     * @param key
     * @param second
     */
    public void expire(byte[] key,Integer second){
        Jedis jedis = null;
        try {
            jedis = pool.getResource();

            jedis.expire(key,second);
        } finally {
            jedis.close();
        }
    }

    /**
     * 从Redis中获取String类型的value
     * @param key
     * @return
     */
    public byte[] get(byte[] key){
        Jedis jedis = null;
        try {
            jedis = pool.getResource();

            byte[] value = jedis.get(key);
            return value;
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除Redis中的key
     * @param key
     */
    public void del(byte[] key){
        Jedis jedis = null;
        try {
            jedis = pool.getResource();

            jedis.del(key);
        } finally {
            jedis.close();
        }
    }

}
